package cn.com.shopec.erp.product.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.LogicUtil;
import cn.com.shopec.erp.common.utils.StringUtil;
import cn.com.shopec.erp.product.commo.ProdSupplierConvertor;
import cn.com.shopec.erp.product.dao.IProdSupplierDao;
import cn.com.shopec.erp.product.entity.ProdSupplier;
import cn.com.shopec.erp.product.service.IProdSupplierService;
import cn.com.shopec.erp.product.vo.ProdSupplierVo;

/**
 * @ClassName: ProdSupplierServiceImpl
 * @Description: 货品供应商价格Service接口实现
 * @author: gaoyaxiong
 * @date: 2013-6-2 下午14:14:13
 */
@Service
public class ProdSupplierServiceImpl implements IProdSupplierService {

    @Resource
    private IProdSupplierDao prodSupplierDao;

    @Resource
    private ProdSupplierConvertor prodSupplierConvertor;

    @Override
    public PageFinder<ProdSupplierVo> getPageFinderProdSupplier(ProdSupplierVo prodSupplierVo, PageQuery pageQuery) {
        DetachedCriteria criteria = this.prodSupplierDao.createCriteria();
        if (LogicUtil.isNotNull(prodSupplierVo)) {
            if (StringUtil.isExist(prodSupplierVo.getProdNo())) {
                criteria.add(Restrictions.eq("prodNo", prodSupplierVo.getProdNo()));
            }
            if (StringUtil.isExist(prodSupplierVo.getSupplierNo())) {
                criteria.add(Restrictions.eq("supplierNo", prodSupplierVo.getSupplierNo()));
            }

            if (null != prodSupplierVo.getIsReturnedGoos()) {
                criteria.add(Restrictions.eq("isReturnedGoos", SZSCMConstant.IS_RETURNED_GOODS));
            }
            if (StringUtil.isExist(prodSupplierVo.getAccount())) {
                String sql = "supplier_no in(select t.code from TBL_SUPPLIER_INFO t where t.account like '%"
                        + prodSupplierVo.getAccount() + "%' and t.delete_flag=1)";
                criteria.add(Restrictions.sqlRestriction(sql));
            }

            if (StringUtil.isExist(prodSupplierVo.getSupplierName())) {
                String sql = "supplier_no in(select t.sup_code from TBL_SUPPLIER_INFO t where t.sup_name like '%"
                        + prodSupplierVo.getSupplierName() + "%' and t.delete_flag=1)";
                criteria.add(Restrictions.sqlRestriction(sql));
            }
        }
        criteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
        PageFinder<ProdSupplier> pageFinder = this.prodSupplierDao.pagedByCriteria(criteria, pageQuery.getPage(),
                pageQuery.getPageSize());
        return this.prodSupplierConvertor.getPageFinderProdSupplierVo(pageFinder);
    }
    
    @Override
    public PageFinder<ProdSupplierVo> getPageFinderProdSupplierST(ProdSupplierVo prodSupplierVo, PageQuery pageQuery) {
        DetachedCriteria criteria = this.prodSupplierDao.createCriteria();
        if (LogicUtil.isNotNull(prodSupplierVo)) {
            if (StringUtil.isExist(prodSupplierVo.getProdNo())) {
                criteria.add(Restrictions.eq("prodNo", prodSupplierVo.getProdNo()));
            }
            if (StringUtil.isExist(prodSupplierVo.getSupplierName())) {
            	String sql = "supplier_no in (select t.code from TBL_SUPPLIER_INFO t where t.sup_name like '%"+prodSupplierVo.getSupplierName()+"%')";
                criteria.add(Restrictions.sqlRestriction(sql));
            }
            if (StringUtil.isExist(prodSupplierVo.getAccount())) {
            	String sql = "supplier_no in (select t.code from TBL_SUPPLIER_INFO t where t.account = '"+prodSupplierVo.getAccount()+"')";
                criteria.add(Restrictions.sqlRestriction(sql));
            }
        }
        criteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
        PageFinder<ProdSupplier> pageFinder = this.prodSupplierDao.pagedByCriteria(criteria, pageQuery.getPage(),
                pageQuery.getPageSize());
        return this.prodSupplierConvertor.getPageFinderProdSupplierVo(pageFinder);
    }

    @Override
    public List<ProdSupplier> getProdSupplierList(ProdSupplierVo prodSupplierVo) {
        DetachedCriteria criteria = this.prodSupplierDao.createCriteria();
        if (LogicUtil.isNotNull(prodSupplierVo)) {
            if (StringUtil.isExist(prodSupplierVo.getProdNo())) {
                criteria.add(Restrictions.eq("prodNo", prodSupplierVo.getProdNo()));
            }
            if (StringUtil.isExist(prodSupplierVo.getSupplierNo())) {
                criteria.add(Restrictions.eq("supplierNo", prodSupplierVo.getSupplierNo()));
            }
        }
        criteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
        List<ProdSupplier> prodSupplierList = this.prodSupplierDao.findByCriteria(criteria);
        return prodSupplierList;
    }

    @Override
    @Transactional
    public String updateProdSupplierByDelectFlag(ProdSupplierVo prodSupplierVo) throws Exception {
        List<ProdSupplier> prodSupplierList = this.getProdSupplierList(prodSupplierVo);
        if (prodSupplierList.size() > 0) {
            ProdSupplier prodSupplier = prodSupplierList.get(0);
            prodSupplier.setDeleteFlag(SZSCMConstant.DELETE_FLAG_YES);
            this.prodSupplierDao.update(prodSupplier);
        }
        return SZSCMConstant.SUCCESS;
    }
    
    @Override
    @Transactional
    public void updateProdSupplierByProdNo(String oldProdNo,String newProdNo){
    	this.prodSupplierDao.updateProdSuppliers(oldProdNo, newProdNo);
    }

	@Override
	public ProdSupplierVo getPuNoByPurchaseRequest(String supName) {
		return prodSupplierDao.getPuNoByPurchaseRequest(supName);
	}
    
}
